package game.view.scene
{
	import com.greensock.events.LoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.Timer;
	
	import game.managers.LanguageManager;
	import game.managers.StageProxy;
	import game.utils.MassLoader;
	import game.utils.Utils;
	
	public class LoadingView extends Sprite
	{
		/**load完的回调函数*/
		private var _completeFun:Function=null;
		/**加载列表*/
		private var _resList:Array;
		
		private var _loader:MassLoader=MassLoader.getInstance();
		
		[Embed("bar.png")]
		private static const Bar:Class;
		
		[Embed("barBG.png")]
		private static const BarBG:Class;
		
		private var bar2:Bitmap;
		private var bg2:Bitmap;
		
		private var totalTx:TextField; //= Utils.getTextField("");
		private var childTx:TextField; //= Utils.getTextField("");
		
		private var _timer:Timer;
		public function LoadingView()
		{
			init();
		}
		
		
		private function init():void
		{	
			bg2=new BarBG();
			bg2.x=(width / 2 - 50) >> 1;
			bg2.y=350;
			addChild(bg2);
			
			
			bar2=new Bar();
			bar2.x=bg2.x + 14;
			bar2.y=bg2.y + 7;
			bar2.width=0;
			addChild(bar2);
			
			childTx=Utils.getCommonTextField("当前进度：0%", 12, 0x000000);
			childTx.x=bg2.x + 410;
			childTx.y= bg2.y + 30;
			addChild(childTx);
			
			/**方便调试,暂时设置为1秒,以后再改*/
			_timer=new Timer(1000,1);
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER,completeHandler);
		}
		
		public function layout():void
		{
			x=(StageProxy.width / 2) >> 1;
			//	y=(StageProxy.height / 2) - 250 >> 1;
		}
		
		
		public function loadRes(arr:Array, fun:Function=null):void
		{
			_completeFun=fun;
			_resList=arr;
			
			_loader.progress=progressHandler;
			_loader.append(_resList);
			addChild(_loader.loader.getContent("loading"));
			
		}
		
		
		private function progressHandler(e:LoaderEvent):void
		{
			//488为图片的宽度
			bar2.width=e.target.progress * 488;
			//bar1.width = e.target.bytesLoaded / e.target.bytesTotal;
			childTx.text="当前进度:" + int(e.target.progress * 100) + "%"
		}
		
		
		private function completeHandler(e:TimerEvent):void
		{
			_timer.removeEventListener(TimerEvent.TIMER,completeHandler);
			_timer=null;
			
			if (_completeFun != null)
			{
				_completeFun();
			}
		}
		
		
		public function dispose():void
		{
			while (numChildren > 0)
				removeChildAt(0);
			if (parent)
			{
				parent.removeChild(this);
			}
			bar2=null;
			bg2=null;
			childTx=null;
		}
		
	}
}